//
//  RecommendTripViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RecommendTripViewController.h"
#import "AFNetWorkingYU.h"
#import "TripModel.h"
#import "RecommendTripCell.h"
#import "UIImageView+WebCache.h"
#import "SightseeingWebView.h"
#import "MJRefresh.h"
#import "NightManager.h"
@interface RecommendTripViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrayOfEditorTrip;
@property (nonatomic, retain) NSMutableArray *arrayOfUserTrip;
@property (nonatomic, copy) NSString *recommemdType;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) UISegmentedControl *segementControl;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, assign) int editorPage;
@property (nonatomic, assign) int userPage;
@property (nonatomic, assign) BOOL ifFirst;
@property (nonatomic, assign) int count;
@end

@implementation RecommendTripViewController
- (void)dealloc
{
    [_activityIndicatorView release];
    [_areaId release];
    [_tableView release];
    [_arrayOfUserTrip release];
    [_arrayOfEditorTrip release];
    [_recommemdType release];
    [_dataArray release];
    [_segementControl release];
    [_url release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfEditorTrip = [NSMutableArray array];
        self.arrayOfUserTrip = [NSMutableArray array];
        self.dataArray = [NSMutableArray array];
        self.recommemdType = @"editor";
        self.url = [NSString string];
        self.editorPage = self.userPage = 1;
        self.ifFirst = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 旋转进度轮
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 150, 100, 100)] ;
    [self.activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicatorView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.activityIndicatorView];
    [_activityIndicatorView release];
    
    // 获取数据
    [self getData];
    
    self.segementControl = [[UISegmentedControl alloc] initWithItems:@[@"编辑推荐",@"用户精选"]];
    self.segementControl.selectedSegmentIndex = 0;
    self.navigationItem.titleView = self.segementControl;
    [self.segementControl addTarget:self action:@selector(changeRecommendType:) forControlEvents:UIControlEventValueChanged];
}
- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self setupRefresh];
    
}

- (void)getData
{
    if (self.ifFirst == YES) {
        [self.activityIndicatorView startAnimating];
        self.dataArray = self.arrayOfEditorTrip;
    }
    // 国家推荐行程
    if (self.areaType == NO) {
        self.url = [NSString stringWithFormat:@"http://open.qyer.com/place/common/get_recommend_plan_list?app_installtime=1429487962&cityid=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88310492374328&lon=121.5399865238222&page=%d&recommand=%@&type=city&v=1",self.areaId,self.editorPage,self.recommemdType];
    }else{
        self.url = [NSString stringWithFormat:@"http://open.qyer.com/place/common/get_recommend_plan_list?app_installtime=1429487962&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&lat=38.88307393003751&lon=121.5399246767973&page=%d&recommand=%@&type=country&v=1",self.areaId,self.userPage,self.recommemdType];
    }
    self.count = self.dataArray.count;
    
    // 请求数据
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:self.url resultBlock:^(id result) {
        
        NSArray *tripArray = result[@"data"];
        if ([self.recommemdType isEqualToString:@"editor"]) {
            for (NSDictionary *dic in tripArray) {
                TripModel *trip = [[TripModel alloc]init];
                [trip setValuesForKeysWithDictionary:dic];
                [self.arrayOfEditorTrip addObject:trip];
                [trip release];
            }
        }else{
            for (NSDictionary *dic in tripArray) {
                TripModel *trip = [[TripModel alloc]init];
                [trip setValuesForKeysWithDictionary:dic];
                [self.arrayOfUserTrip addObject:trip];
                [trip release];
            }
        }
        if (self.userPage == 1) {
            [self createView];
        }
        if (self.editorPage == 1) {
            self.tableView.hidden = NO;
        }

        if (self.count == self.dataArray.count) {
            self.tableView.footerPullToRefreshText = @"没有更多内容了!";
            self.tableView.footerReleaseToRefreshText = @"没有更多内容了!";

        }
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
        [self.activityIndicatorView stopAnimating];
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count == 0) {
        self.tableView.backgroundColor = [UIColor grayColor];
    }else{
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    RecommendTripCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell  = [[[RecommendTripCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    TripModel *trip = self.dataArray[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:trip.photo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.daysLabel.text = [NSString stringWithFormat:@"旅行天数:%@",trip.day_count];
    cell.tripLabel.text = [NSString stringWithFormat:@"路线:%@",trip.route];

    cell.updateLabel.text = [NSString stringWithFormat:@"更新时间:%@",trip.updatetime];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SightseeingWebView *webVC = [[SightseeingWebView alloc]init];
    webVC.url = [self.dataArray[indexPath.row] view_url];
    webVC.title = @"推荐行程";
    [self.navigationController pushViewController:webVC animated:YES];
    [webVC release];
}
// 切换数组和推荐类型
- (void)changeRecommendType:(UISegmentedControl *)segement
{
    if (segement.selectedSegmentIndex == 0) {
        self.recommemdType = @"editor";
        self.dataArray = self.arrayOfEditorTrip;
    }else{
        self.recommemdType = @"user";
        if (self.ifFirst == YES) {
            self.tableView.hidden = YES;
            [self getData];
        }
        self.ifFirst = NO;
        self.dataArray = self.arrayOfUserTrip;
    }
    [self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
- (void)setupRefresh
{
    [self.tableView addFooterWithCallback:^{
        [self footerRereshing];
        
    }];
}
 - (void)footerRereshing
{
    if (self.areaType == NO) {
        self.editorPage++;

    }else{
        self.userPage++;
    }
    [self getData];
    

}
- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
}
- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
